//
//  LanguageSelectView.m
//  translateAi
//
//  Created by 陈镇 on 2023/5/7.
//

#import "LanguageSelectView.h"
#import "SLXiaLaButton.h"
#import "SLPopMenuView.h"
@interface LanguageSelectView ()
/** 注释*/
@property (nonatomic, strong) SLXiaLaButton * originalBtn;
/** <#注释#>*/
@property (nonatomic, strong) SLXiaLaButton * translationBtn;
/** <#注释#>*/
@property (nonatomic, strong) QMUIButton * changebtn;

@property (nonatomic, strong) NSArray<NSString *> * languageList;


@end

@implementation LanguageSelectView

-(instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        [self addSubview:self.changebtn];
        [self addSubview:self.originalBtn];
        [self addSubview:self.translationBtn];
        
    }
    return self;
}

-(QMUIButton *)changebtn{
    if(!_changebtn){
        _changebtn = [QMUIButton createButtonImageName:@"change" selectImageName:@"change"];
        [_changebtn addTarget:self action:@selector(changeClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _changebtn;
}
-(SLXiaLaButton *)originalBtn{
    if(!_originalBtn){
        _originalBtn = [SLXiaLaButton sl_createButtonTitle:@"中文" titleColor:UIColorHex(#232323) font:SLFontMakeMedium(16) imageName:@"xiala" selectImageName:@"xiala"];
        [_originalBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        _originalBtn.imagePosition = QMUIButtonImagePositionRight;
        _originalBtn.spacingBetweenImageAndTitle = 4;
        _originalBtn.layer.masksToBounds = YES;
        _originalBtn.layer.cornerRadius = 16;
    }
    return _originalBtn;
}
-(SLXiaLaButton *)translationBtn{
    if(!_translationBtn){
        _translationBtn = [SLXiaLaButton sl_createButtonTitle:@"英语" titleColor:UIColorHex(#232323) font:SLFontMakeMedium(16) imageName:@"xiala" selectImageName:@"xiala"];
        [_translationBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        _translationBtn.imagePosition = QMUIButtonImagePositionRight;
        _translationBtn.spacingBetweenImageAndTitle = 4;
        _translationBtn.layer.masksToBounds = YES;
        _translationBtn.layer.cornerRadius = 16;
    }
    return _translationBtn;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    _changebtn.frame = CGRectMake((self.width - 22)/2, 20, 22, 15);
    
    _originalBtn.frame = CGRectMake(_changebtn.left - 16 - 90, 0, 90, self.height);
    _translationBtn.frame = CGRectMake(_changebtn.right + 16, _originalBtn.top, _originalBtn.width, _originalBtn.height);
    
}

#pragma mark - Action
-(void)clickBtn:(UIButton *)btn{
    __weak typeof(self) weakSelf = self;
    [CurrentVC.view endEditing:YES];
    [SLPopMenuView showMenuWithView:btn titles:self.languageList imageNames:@[] block:^(XHPopMenuItem * _Nullable item) {
            
        if(btn == weakSelf.originalBtn){
            weakSelf.originalBtn.titleString = item.title;
        }else{
            weakSelf.translationBtn.titleString = item.title;
        }
        
        if(weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(selector_code:ta_code:)]){
            [weakSelf.delegate selector_code:weakSelf.or_code ta_code:weakSelf.ta_code];
        }
        RTLog(@"%@",item.title);
    }];
}
-(void)settingOrTitle:(NSString *)orTitle taTitle:(NSString *)taTitle{
    self.originalBtn.titleString = orTitle;
    self.translationBtn.titleString = taTitle;
}
-(void)changeClick{
    NSString *tmpText = self.originalBtn.titleString;
    self.originalBtn.titleString = self.translationBtn.titleString;
    self.translationBtn.titleString = tmpText;
}

-(NSString *)or_code{
    return [self getLanguageCode:self.originalBtn.titleString];
}
-(NSString *)ta_code{
    return [self getLanguageCode:self.translationBtn.titleString];
}
@end






@implementation LanguageSelectView (country)

#pragma mark - 文字翻译code 对照及获取
-(NSString *)getLanguageCode:(NSString *)language{
    
    NSString *languageCode = @"en";
    
    NSInteger index = [self.languageList indexOfObject:language];
    NSDictionary *dic = [[LanguageSelectView getLanguageAnalysisListWithType:self.type] objectAtIndex:index];
    
    languageCode = dic[language];
    
    

   
    return languageCode;
}

-(NSArray<NSString *> *)languageList{
    
    NSMutableArray *list = [NSMutableArray array];
    for (NSDictionary *dic in [LanguageSelectView getLanguageAnalysisListWithType:self.type]) {
        NSString *title = dic.allKeys.firstObject;
        [list addObject:title];
        
    }
    _languageList = list;
    return _languageList;
}


+(NSArray<NSString *> *)mylanguageList:(NSString *)type{
    
    NSMutableArray *list = [NSMutableArray array];
    for (NSDictionary *dic in [LanguageSelectView getLanguageAnalysisListWithType:type]) {
        NSString *title = dic.allKeys.firstObject;
        [list addObject:title];
        
    }
    return list;
}

+(NSString *)getLanguageCode:(NSString *)language type:(NSString *)type{
    
    NSString *languageCode = @"en";
    
    NSInteger index = [[LanguageSelectView mylanguageList:type] indexOfObject:language];
    NSDictionary *dic = [[LanguageSelectView getLanguageAnalysisListWithType:type] objectAtIndex:index];
    
    languageCode = dic[language];
    
    

   
    return languageCode;
}

+(NSArray *)getLanguageAnalysisListWithType:(NSString *)type{
    if ([type isEqualToString:@"text"]) {
        return [LanguageSelectView textLanguageAnalysisList];
    }else if ([type isEqualToString:@"image"]) {
        return [LanguageSelectView imageLanguageAnalysisList];
    }else{
        return [LanguageSelectView voiceLanguageAnalysisList];
    }
}


+(NSArray *)voiceLanguageAnalysisList{
    
    return @[@{@"中文":@"zh-CHS"},
             @{@"英语":@"en"},
             @{@"英文（澳大利亚）":@"en-AUS"},
             @{@"英文（英国）":@"en-GBR"},
             @{@"英文（印度）":@"en-IND"},
             @{@"日文":@"ja"},
             @{@"韩文":@"ko"},
             @{@"法文":@"fr"},
             @{@"西班牙文":@"es"},
             @{@"葡萄牙文":@"pt"},
             @{@"俄文":@"ru"},
             @{@"德文":@"de"},
             @{@"阿拉伯文":@"ar"},
             @{@"印尼文":@"id"},
             @{@"加泰隆语":@"ca"},
             @{@"捷克语":@"cs"},
             @{@"丹麦语":@"da"},
             @{@"希腊语":@"el"},
             @{@"芬兰语":@"fi"},
             @{@"法语（加拿大）":@"fr-CAN"},
             @{@"希伯来语":@"he"},
             @{@"印地语":@"hi"},
             @{@"匈牙利语":@"hu"},
             @{@"意大利语":@"it"},
             @{@"荷兰语":@"nl"},
             @{@"挪威语":@"no"},
             @{@"波兰语":@"pl"},
             @{@"葡萄牙语（巴西）":@"pt-BRA"},
             @{@"罗马尼亚语":@"ro"},
             @{@"斯洛伐克语":@"sk"},
             @{@"瑞典语":@"sv"},
             @{@"泰语":@"th"},
             @{@"土耳其语":@"tr"},
             @{@"粤语":@"yue"},
             @{@"普通话（中国台湾）":@"zh-TWN"},
    ];
}

+(NSArray *)textLanguageAnalysisList{
    return @[@{@"中文":@"zh-CHS"},
             @{@"英语":@"en"},
             @{@"繁体中文":@"zh-CHT"},
             @{@"阿拉伯语":@"ar"},
             @{@"德语":@"de"},
             @{@"西班牙语":@"es"},
             @{@"法语":@"fr"},
             @{@"印地语":@"hi"},
             @{@"印度尼西亚语":@"id"},
             @{@"意大利语":@"it"},
             @{@"日语":@"ja"},
             @{@"韩语":@"ko"},
             @{@"荷兰语":@"nl"},
             @{@"葡萄牙语":@"pt"},
             @{@"俄语":@"ru"},
             @{@"泰语":@"th"},
             @{@"越南语":@"vi"},
             
             @{@"南非荷兰语":@"af"},
             @{@"阿姆哈拉语":@"am"},
             @{@"阿塞拜疆语":@"az"},
             @{@"白俄罗斯语":@"be"},
             
             @{@"保加利亚语":@"bg"},
             @{@"孟加拉语":@"bn"},
             @{@"波斯尼亚语":@"bs"},
             @{@"加泰隆语":@"ca"},
             
             @{@"宿务语":@"ceb"},
             @{@"科西嘉语":@"co"},
             @{@"捷克语":@"cs"},
             @{@"威尔士语":@"cy"},
             
             @{@"丹麦语":@"da"},
             @{@"希腊语":@"el"},
             @{@"世界语":@"eo"},
             @{@"爱沙尼亚语":@"et"},
             
             @{@"波斯语":@"fa"},
             @{@"芬兰语":@"fi"},
             @{@"匈牙利语":@"hu"},
             @{@"老挝语":@"lo"},
             
             @{@"马来语 ":@"ms"},
             @{@"南非祖鲁语":@"zu"},
             @{@"粤语":@"yue"},
    ];

}

+(NSArray *)imageLanguageAnalysisList{
    return [LanguageSelectView textLanguageAnalysisList];
}

@end
